//
//  TimeIntervalPicker.m
//  TableViewApp
//
//  Created by Dmitriy on 18.06.13.
//  Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//

#import "TimeIntervalPicker.h"

@implementation TimeIntervalPicker
@synthesize interval = _interval;

-(id) init
{
    self = [super init];
    _interval = 0;
    return self;
}
-(void) dealloc
{
    [super dealloc];
}

- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 4;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return 31;
        case 1:
            return 24;
        case 2:
            return 60;
        case 3:
            return 60;
            
        default:
            return 0;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *appendix;
    switch (component) {
        case 0:
            appendix = @"d"; break;
        case 1:
            appendix = @"h"; break;
        case 2:
            appendix = @"m"; break;
        case 3:
            appendix = @"s"; break;            
        default:
            appendix = @""; break;
    }
    return [[[NSNumber numberWithInt:row] stringValue] stringByAppendingString:appendix];
}

-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    const int SECONDS_IN_MINUTE = 60;
    const int SECONDS_IN_HOUR = SECONDS_IN_MINUTE*60;
    const int SECONDS_IN_DAY = SECONDS_IN_HOUR*24;
    _interval = [pickerView selectedRowInComponent:0]*SECONDS_IN_DAY+[pickerView selectedRowInComponent:1]*SECONDS_IN_HOUR +[pickerView selectedRowInComponent:2]*SECONDS_IN_MINUTE + [pickerView selectedRowInComponent:3];
}

-(BOOL)showsSelectionIndicator
{
    return YES;
}

@end
